package com.example.erp.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.example.erp.config.CommonConfig;
import io.jsonwebtoken.Claims;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

public class DataFormatUtils {
    public static <T> String getStrOfList(List<T> list) {
        if (CollectionUtil.isEmpty(list)) {
            return "";
        }
        return JSONUtil.toJsonStr(list);
    }

    public static <T> List<T> getListOfStr(String str, Class<T> clazz) {
        if (StrUtil.isEmpty(str)) {
            return new ArrayList<>();
        }
        return JSONUtil.toList(str, clazz);
    }

    public static boolean isAllowedFileType(String extension) {
        Set<String> allowedTypes = new HashSet<>(Arrays.asList(
                "jpg", "jpeg", "png" , "blob"
        ));
        return allowedTypes.contains(extension);
    }

    public static String getFileExtension(String filename) {
        int dotIndex = filename.lastIndexOf('.');
        return (dotIndex == -1) ? "png" : filename.substring(dotIndex + 1).toLowerCase();
    }

    public static Long getTokenUser(String token, String key) {
        Claims claims = JwtUtils.parseJwt(token);
        //解析token的id
        return claims.get(key, Long.class);
    }

    public static String generateOrderNo(CommonConfig.OrderType orderType, int status) {
        // 确定前缀
        String prefix = determinePrefix(orderType, status);
        // 日期部分 (6位)
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
        String dateStr = dateFormat.format(new Date());
        // 随机数部分 (8位)
        Random random = new Random();
        String randomPart = random.ints(8, 0, 10)
                .mapToObj(String::valueOf)
                .collect(Collectors.joining());
        // 组合16位订单号
        return prefix + dateStr + randomPart;
    }
    private static String determinePrefix(CommonConfig.OrderType orderType, int status) {
        switch (orderType) {
            case PURCHASE:
            case SALES:
                return orderType.getPrefixes()[0];
            case INBOUND:
                if (status == CommonConfig.compare.STATUSONE.getDescription()) {
                    return orderType.getPrefixes()[0]; // RK
                } else if (status == CommonConfig.compare.STATUSTWO.getDescription()) {
                    return orderType.getPrefixes()[1]; // TH
                } else {
                    return orderType.getPrefixes()[2]; // QT
                }
            case OUTBOUND:
                if (status == CommonConfig.compare.STATUSONE.getDescription()) {
                    return orderType.getPrefixes()[0]; // CK
                } else if (status == CommonConfig.compare.STATUSTWO.getDescription()) {
                    return orderType.getPrefixes()[1]; // TH
                } else {
                    return orderType.getPrefixes()[2]; // QT
                }
            default:
                throw new IllegalArgumentException("Invalid order type");
        }
    }
}
